<template>
  <div class="main-container">
    <a-card class="layout-card">
      <template #title>
        <span style="font-weight: 700">系统配置</span>
      </template>
      <a-form
        class="form"
        :model="addSubjectForm"
        name="basic"
        @submit="subFuncatio"
        ref="formRef"
        :rules="rules"
      >
        <a-row :gutter="24">
          <a-col :span="10">
            <a-form-item label="无操作自动退出系统时间(分钟)" name="noActionLogoutMinutes">
              <a-tooltip
                :key="'#fadfvf'"
                title="默认30分钟, 0表示不限制时长"
                :color="'#fadfvf'"
                class="mr-10px"
              >
                <QuestionCircleOutlined />
              </a-tooltip>
              <a-input-number
                :disabled="setStater"
                v-model:value="addSubjectForm.noActionLogoutMinutes"
                :min="0"
                placeholder="请输入时间（分钟）"
                style="width: calc(100% - 50px)"
              />
            </a-form-item>
          </a-col>
          <a-col :span="10">
            <a-form-item label="允许输入错误密码的最大次数" name="allowedErrorPwdMax">
              <a-tooltip
                :key="'#fadfvf'"
                title="默认3次, 0表示不限制次数"
                :color="'#fadfvf'"
                class="mr-10px"
              >
                <QuestionCircleOutlined />
              </a-tooltip>
              <a-input-number
                :disabled="setStater"
                v-model:value="addSubjectForm.allowedErrorPwdMax"
                :min="0"
                placeholder="请输入允许错误密码最大次数"
                style="width: calc(100% - 50px)"
              />
            </a-form-item>
          </a-col>
          <a-col :span="10">
            <a-form-item label="评阅前页面锁定时长(秒)" name="pyLoadingSeconds">
              <a-tooltip
                :key="'#fadfvf'"
                title="设置评阅前页面锁定时长，一定程度上防止评阅教师随意提交"
                :color="'#fadfvf'"
                class="mr-10px"
              >
                <QuestionCircleOutlined />
              </a-tooltip>
              <a-input-number
                :disabled="setStater"
                v-model:value="addSubjectForm.pyLoadingSeconds"
                :min="0"
                placeholder="请输入评阅锁定时长（秒）"
                style="width: calc(100% - 50px)"
              />
            </a-form-item>
          </a-col>
          <a-col :span="10">
            <a-form-item label="显示考生信息" name="showKsxxInTables">
              <a-tooltip
                :key="'#fadfvf'"
                title="开启后，在部分表单页面上展示考生信息"
                :color="'#fadfvf'"
                class="mr-10px"
              >
                <QuestionCircleOutlined />
              </a-tooltip>
              <a-switch
                :disabled="setStater"
                v-model:checked="addSubjectForm.showKsxxInTables"
                checked-children="开"
                un-checked-children="关"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="新增教师信息是否开启校验" name="operateAccountCheckFlag">
              <a-tooltip
                :key="'#fadfvf'"
                title="作用于新增教师账号时，判断是否需要进行教师信息校验和导入教师校验库的功能需进行匹配"
                :color="'#fadfvf'"
                class="mr-10px"
              >
                <QuestionCircleOutlined />
              </a-tooltip>
              <a-switch
                :disabled="setStater"
                v-model:checked="addSubjectForm.operateAccountCheckFlag"
                checked-children="开"
                un-checked-children="关"
              />
            </a-form-item>
          </a-col>

          <a-col :span="24"></a-col>

          <a-col :span="24" style="text-align: center; margin-top: 30px">
            <a-button type="primary" :loading="subloading" :disabled="setStater" html-type="submit">
              保存
            </a-button>
            <a-button type="" @click="setPageStale" style="margin-left: 30px">
              {{ setStater ? '编辑' : '取消' }}
            </a-button>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { setConfigInfo } from '/@/api/system-manage/teacher-info'

import { useUserStoreWithOut } from '/@/store/modules/user'
const userStore = useUserStoreWithOut()
const addSubjectForm = computed(() => userStore.getSysConfig)

import { useMessage } from '/@/hooks/web/useMessage'

const { createMessage } = useMessage()
const setStater = ref<Boolean>(true)
const subloading = ref<Boolean>(false)

let kgtPyTypeOptions = ref<any>([])
let fpOpenOptions = ref<any>([])
let zjDataOptions = ref<any>([])

const rules = {
  // pyLoadingSeconds: [
  //   {
  //     required: true,
  //     validator: (_rule, value, _callback) => {
  //       if (value <= 0) {
  //         return Promise.reject('评阅前页面锁定时间需要大于0')
  //       } else {
  //         return Promise.resolve()
  //       }
  //     },
  //     trigger: 'change',
  //   },
  //   { required: true, message: '评阅前页面锁定时间不能为空', trigger: 'blur' },
  // ],
}

const setPageStale = () => {
  if (!setStater.value) {
    userStore.setSysConfig()
  }
  setStater.value = !setStater.value
}
const formRef = ref()
/**
 * 设置评阅设置
 */
const subFuncatio = async () => {
  formRef.value.validateFields().catch(err => {
    if (!err.errorFields.length) {
      subloading.value = true
      setConfigInfo(addSubjectForm.value)
        .then((res: any) => {
          if (res.code == 200) {
            createMessage.success('保存成功')
            setStater.value = true
          } else {
            createMessage.error(res.message)
          }
        })
        .finally(() => {
          subloading.value = false
        })
    }
  })
}

import { getDictionaryList } from '/@/views/teacher/home/index'

onMounted(async () => {
  // 获取评阅设置

  await getDictionaryList('kgtPyType', kgtPyTypeOptions)
  await getDictionaryList('fpDataType', fpOpenOptions)
  await getDictionaryList('zjDataType', zjDataOptions)
})
</script>

<style lang="less" scoped>
.main-container {
  // height: @c_height;
  .layout-card {
    height: 100%;
    border-radius: 8px;
    margin-bottom: 30px;
  }
}
</style>
